package com.glory.hikvisionmobile.base;

import android.app.Activity;
import android.content.Context;
import android.widget.EditText;

import com.glory.hikvisionmobile.activity.LoginActivity;
import com.glory.hikvisionmobile.bean.Login.AuthoritysBean;
import com.glory.hikvisionmobile.bean.Login.LoginInfoBean;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.utils.EditTextEnterUtils;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ActivityManager;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


public abstract class BaseActivity extends FreeUI_BaseActivity {
    private FreeApi_ActivityManager activityManager;
    private LoginInfoBean bean;
    public static Map<String,Map<String, AuthoritysBean>> Jurisdiction= new LinkedHashMap<>();

    public interface editTextListener{
        void onTextChangeListener(String s);
    }

    public interface ActionControlListener{
        void onControlChangeListener();
    }
    @Override
    protected abstract int getLayoutResId();

    @Override
    protected abstract void initView();

    @Override
    protected abstract void initData();

    protected void initPermission(String tag, Context context,ActionControlListener listener){
//        String ss = "{\n" +
//                "\t\"Header\": {\n" +
//                "\t\t\"TRANSACTIONID\": \"c5e5e318-389d-4c39-b914-e4c10ba519d4\",\n" +
//                "\t\t\"RESULT\": \"SUCCESS\"\n" +
//                "\t},\n" +
//                "\t\"Body\": {\n" +
//                "\t\t\"authoritys\": [{\n" +
//                "\t\t\t\"tabKey\": \"PDALotFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Lot_LotScrapCancelActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Submit;Refresh;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAFormFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"From_CheckPlanFromActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Refresh;Submit;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAFormFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"From_IQPCRecordActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Submit;Refresh;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAWorkFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"DialogActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAWorkFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Work_TrackInActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Submit;Refresh;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAWorkFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Work_GraphiteBoatTrackOutActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"TrackOut;TrackExit;Add;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAEqpFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Eqp_EqpCheckActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Refresh;Submit;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAWorkFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Work_SPCDataCollectActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Refresh;TemporarySave;Submit;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAWorkFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Work_GraphiteBoatTrackInActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Submit;Refresh;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAWorkFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Work_DataCollectActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Search;Submit;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDALotFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Lot_LotScrapActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Submit;Refresh;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAWorkFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Work_BindCarrierActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Submit;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAFormFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"From_ReplaceRecordActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Refresh;Submit;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDALotFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Lot_SearchWorkThingActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Search;Refresh;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDALotFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Lot_LotGoActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Submit;Refresh;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAFormFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"From_AddDrugRecordActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Submit;Refresh;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAEqpFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Eqp_EqpKeepDetailsActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Refresh;Submit;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDALotFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Lot_LotSplitActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Submit;Refresh;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAWorkFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Work_TrackOutActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Scrap;TrackExit;SPCDataCollect;TrackOut;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAFormFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"From_MaterielRecordActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Submit;Refresh;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDALotFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Lot_LotPauseActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Refresh;Submit;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAWorkFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Work_PatchTrackOutActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"TrackOut;DieBond;TrackExit;BindCarrier;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDALotFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Lot_LotBackStepActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Refresh;Submit;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAFormFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"From_FromSearchActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"NewBuild;Search;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDALotFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Lot_LotAddActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Submit;Refresh;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAFormFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"From_ChangeShiftsActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Refresh;Submit;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAEqpFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Eqp_EqpKeepActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Refresh;Search;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDALotFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Lot_ProcessSheetActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Search;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAWorkFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Work_SortingTrackOutActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"TrackOut;TrackExit;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAFormFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"From_CheckPlanActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Refresh;Search;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAWorkFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Work_MapPingActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Submit;\"\n" +
//                "\t\t}, {\n" +
//                "\t\t\t\"tabKey\": \"PDAWorkFragment\",\n" +
//                "\t\t\t\"funcitonKey\": \"Work_EqpWorkActivity\",\n" +
//                "\t\t\t\"buttonKey\": \"Search;\"\n" +
//                "\t\t}],\n" +
//                "\t\t\"userName\": \"pda\",\n" +
//                "\t\t\"desription\": \"pda\"\n" +
//                "\t}\n" +
//                "}";
//        BaseResponseBean<LoginInfoBean> data = (new Gson()).fromJson(ss, new TypeToken<BaseResponseBean<LoginInfoBean>>() {}.getType());
//        LoginInfoBean body = data.getBody();
//        Jurisdiction.clear();
//        body.getAuthoritys().forEach(s  ->{
//            if(Jurisdiction.containsKey(s.getTabKey())) {
//                Jurisdiction.get(s.getTabKey()).put(s.getFunctionKey(), s);
//            }else{
//                Map<String, AuthoritysBean> beanMap = new HashMap<>();
//                beanMap.put(s.getFunctionKey(),s);
//                Jurisdiction.put(s.getTabKey(),beanMap);
//            }
//        });
//        actionControl(tag);
        if (Constant.isConstantlyControl){
            Jurisdiction.clear();
            Jurisdiction = Constant.Jurisdiction;
            actionControl(tag,listener);
            return;
        }
        JsonObject map = new JsonObject();
        map.addProperty("userName",Constant.CURRENT_USER);
        map.addProperty("password",Constant.CURRENT_PASSWORD);
        map.addProperty("actionType","Login");
        NetUtils.requestDara("HIK.PdaLogin",this,map,new FreeUI_DialogEntityCallBack<BaseResponseBean<LoginInfoBean>>(
                new TypeToken<BaseResponseBean<LoginInfoBean>>() {
                }.getType(), getSupportFragmentManager(), this){

            @Override
            public void onSuccess(Response<BaseResponseBean<LoginInfoBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())) {
                    bean = response.body().getBody();
                    if (bean.getAuthoritys().size() <= 0){
                        showToast("无任何权限",false);
                        FreeApi_Utils.skipWithExtraIntent("header", context,LoginActivity.class);
                        activityManager.finishAllActivity();
                        return;
                    }
                    Jurisdiction.clear();
                    bean.getAuthoritys().forEach(s  ->{
                        if(Jurisdiction.containsKey(s.getTabKey())) {
                            Jurisdiction.get(s.getTabKey()).put(s.getFunctionKey(), s);
                        }else{
                            Map<String, AuthoritysBean> beanMap = new HashMap<>();
                            beanMap.put(s.getFunctionKey(),s);
                            Jurisdiction.put(s.getTabKey(),beanMap);
                        }
                    });

                    actionControl(tag,listener);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<LoginInfoBean>> response) {
                super.onError(response);
                loadError(response.getException(), "HIK.PdaLogin");
            }
        });
    }

    @Override
    protected abstract FreeUI_AddViewEntity getBaseEntity();

    protected boolean editIsEmpty(EditText et,String msg){
        if (et.getText().toString().trim().length() == 0) {
            showToast(msg, false);
            return false;
        } else {
            return true;
        }
    }

    protected void initEdit(EditText editText,editTextListener listener){
        editText.requestFocus();
        EditTextEnterUtils.initEnterToUpperCase(editText);
        FreeApi_EditTextUtils.setEditTextListener(editText, this, s -> {
            listener.onTextChangeListener(s);
        });
    }

    /**
     * 不获取焦点初始化editText
     * @param editText
     * @param listener
     * @param init
     */
    protected void initEdit(EditText editText,editTextListener listener,boolean init){
        EditTextEnterUtils.initEnterToUpperCase(editText);
        FreeApi_EditTextUtils.setEditTextListener(editText, this, s -> {
            listener.onTextChangeListener(s);
        });
    }

    protected void showConfirmDialog(Activity context,String message){
        FreeApi_DialogUtils.getConfirmDialog(this, "确定",message, () -> {
            context.finish();
        }).show(getSupportFragmentManager(),"CONFIRM_DIALOG");
    }

    protected void showConfirmDialog(String message){
        FreeApi_DialogUtils.getConfirmDialog(this, "确定",message, () -> {

        }).show(getSupportFragmentManager(),"CONFIRM_DIALOG");
    }


    private void actionControl(String tag,ActionControlListener listener)
    {
        if (Jurisdiction.containsKey(Constant.CURRENTFRAGMENTNAME)){
            if (Jurisdiction.get(Constant.CURRENTFRAGMENTNAME).containsKey(getClass().getSimpleName())){
                if (Jurisdiction.get(Constant.CURRENTFRAGMENTNAME).get(getClass().getSimpleName()).getButtonKey().contains(tag)){
                    listener.onControlChangeListener();
                    return;
                }
            }
        }

        FreeUI_GeneralFragmentDialog confirmDialog = FreeApi_DialogUtils.getConfirmDialog(this, "确定", "无权限", () -> {

        });
        confirmDialog.setCancelable(false);
        confirmDialog.show(getSupportFragmentManager(),"AUTH_DIALOG");
    }

    protected boolean actionControl(String activity,String fragment,String message){

        if(Constant.Jurisdiction.get(fragment).containsKey(activity)) {
            return false;
        }else{
            FreeApi_DialogUtils.getConfirmDialog(this, "确定",message, () -> {

            }).show(getSupportFragmentManager(),"AUTH_DIALOG");
            return true;
        }

    }

    protected boolean actionControl(String tag,String fragment)
    {
        if(Constant.Jurisdiction.get(fragment).get(getClass().getSimpleName()).getButtonKey().contains(tag)) {
            return false;
        }else{
            FreeApi_DialogUtils.getConfirmDialog(this, "确定","无权限", () -> {

            }).show(getSupportFragmentManager(),"AUTH_DIALOG");
            return true;
        }
    }

    protected boolean actionControl(String tag)
    {
        if(Constant.Jurisdiction.get(Constant.CURRENTFRAGMENTNAME).get(getClass().getSimpleName()).getButtonKey().contains(tag)) {
            return false;
        }else{
            FreeApi_DialogUtils.getConfirmDialog(this, "确定","无权限", () -> {

            }).show(getSupportFragmentManager(),"AUTH_DIALOG");
            return true;
        }
    }

}
